/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.edu.ntu.yyl.controller.admin;

import cn.edu.ntu.yyl.pojo.article.Article;
import cn.edu.ntu.yyl.pojo.article.ArticleCategory;
import cn.edu.ntu.yyl.pojo.article.ArticleComment;
import cn.edu.ntu.yyl.pojo.user.UserInfo;
import cn.edu.ntu.yyl.service.*;
import cn.edu.ntu.yyl.tool.OptionReaderTool;
import cn.edu.ntu.yyl.vo.ArticleVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

import static cn.edu.ntu.yyl.tool.LayuiTool.objectToJSON;

@RestController
public class AdminAction extends HttpServlet {
    @Autowired
    OptionService optionService;
    @Autowired
    OptionReaderTool optionReaderTool;
    @Autowired
    CategoryService categoryService;
    @Autowired
    CommentService commentService;
    @Autowired
    ArticleService articleService;
    @Autowired
    ArticleShowService articleShowService;
    @Autowired
    CommentVOService commentVOService;
    //文章设置
    @RequestMapping("/ArticleSetting.do")
    public String ArticleSetting(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        Map<String,Object> options = optionReaderTool.ReadOptions();
        response.setContentType("text/html;charset=UTF-8");
        options.put("open_commet",request.getParameter("open_commet"));
        options.put("allow_unlogged_user_comment",request.getParameter("allow_unlogged_user_comment"));
        options.put("comment_moderation",request.getParameter("comment_moderation"));
        options.put("article_comment_code",request.getParameter("article_comment_code"));
        options.put("allow_search_article",request.getParameter("allow_search_article"));
        options.put("search_engine",request.getParameter("article_search_engine"));
        optionService.UpdateArticleSetting(options);
        return "redirect: /../admin/article/setting";
    }
    //文章分类
    @RequestMapping("/ArticleCategoryUpdate.do")
    public String ArticleCategoryUpdate(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        //获取时间
        Date date=new Date(System.currentTimeMillis());
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        response.setContentType("text/html;charset=UTF-8");
        ArticleCategory articleCategory = new ArticleCategory();
        //取出表单内容
        if (request.getParameter("category.id")==null){
            articleCategory.setId(-1);
        } else{
            articleCategory.setId(Integer.valueOf(request.getParameter("category.id")));
        }

        articleCategory.setType("classify");
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        articleCategory.setUser_id(userInfo.getId());
        articleCategory.setTitle(request.getParameter("category.title"));
        articleCategory.setPid(Integer.valueOf(request.getParameter("category.pid")));
        articleCategory.setSlug(request.getParameter("category.slug"));
        if (request.getParameter("category.order_number")==null||request.getParameter("category.order_number").equals("")){
            articleCategory.setOrder_number(0);
        } else{
            articleCategory.setOrder_number(Integer.valueOf(request.getParameter("category.order_number")));
        }
        articleCategory.setContent(request.getParameter("category.content"));
        articleCategory.setFlag(request.getParameter("category.flag"));
        articleCategory.setMeta_description(request.getParameter("category.meta_description"));
        articleCategory.setMeta_keywords(request.getParameter("category.meta_keywords"));
        if (articleCategory.getId()>=1){
            new ArticleCategory();
            ArticleCategory category;
            //取出修改前的内容
            category = categoryService.SelectCategoryById(articleCategory.getId());
            //用修改后的内容覆盖掉修改前的内容
            category.setTitle(articleCategory.getTitle());
            category.setPid(articleCategory.getPid());
            category.setSlug(articleCategory.getSlug());
            category.setOrder_number(articleCategory.getOrder_number());
            category.setContent(articleCategory.getContent());
            category.setFlag(articleCategory.getFlag());
            category.setMeta_description(articleCategory.getMeta_description());
            category.setMeta_keywords(articleCategory.getMeta_keywords());
            category.setModified(date);
            //更新数据库
            categoryService.UpdateArticleCategory(category);
        } else {
            //id小于1则表示为添加
            articleCategory.setCreated(date);
            categoryService.AddNewArticleCategory(articleCategory);
        }
        return "redirect: /../admin/article/category";
    }
    //文章标签
    @RequestMapping("/ArticleTagUpdate.do")
    public String ArticleTagUpdate(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        //获取时间
        Date date=new Date(System.currentTimeMillis());
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        response.setContentType("text/html;charset=UTF-8");
        ArticleCategory articleCategory = new ArticleCategory();
        //取出表单内容
        if (request.getParameter("category.id")==null){
            articleCategory.setId(-1);
        } else{
            articleCategory.setId(Integer.valueOf(request.getParameter("category.id")));
        }

        articleCategory.setType("tag");
        UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");
        articleCategory.setUser_id(userInfo.getId());
        articleCategory.setTitle(request.getParameter("category.title"));
        articleCategory.setPid(0);
        articleCategory.setSlug(request.getParameter("category.slug"));
        if (request.getParameter("category.order_number")==null||request.getParameter("category.order_number").equals("")){
            articleCategory.setOrder_number(0);
        } else{
            articleCategory.setOrder_number(Integer.valueOf(request.getParameter("category.order_number")));
        }
        articleCategory.setContent(request.getParameter("category.content"));
        articleCategory.setFlag(request.getParameter("category.flag"));
        articleCategory.setSummary(request.getParameter("category.summary"));
        String Onum = request.getParameter("category.order_number");
        articleCategory.setOrder_number(Integer.valueOf(Onum));
        articleCategory.setMeta_description(request.getParameter("category.meta_description"));
        articleCategory.setMeta_keywords(request.getParameter("category.meta_keywords"));
        if (articleCategory.getId()>=1){
            ArticleCategory category;
            //取出修改前的内容
            category = categoryService.SelectTagById(articleCategory.getId());
            //用修改后的内容覆盖掉修改前的内容
            category.setTitle(articleCategory.getTitle());
            category.setPid(articleCategory.getPid());
            category.setSummary(articleCategory.getSummary());
            category.setSlug(articleCategory.getSlug());
            category.setOrder_number(articleCategory.getOrder_number());
            category.setContent(articleCategory.getContent());
            category.setFlag(articleCategory.getFlag());
            category.setMeta_description(articleCategory.getMeta_description());
            category.setMeta_keywords(articleCategory.getMeta_keywords());
            category.setModified(date);
            //更新数据库
            categoryService.UpdateTag(category);
        } else {
            //id小于1则表示为添加
            articleCategory.setCreated(date);
            categoryService.AddNewTag(articleCategory);
        }
        return "redirect: /../admin/article/tag";
    }
    //编辑评论
    @RequestMapping("/ArticleCommentUpdate.do")
    public String ArticleCommentUpdate(HttpSession session, Model model, HttpServletRequest request){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //取出表单内容
        if (request.getParameter("editcomment")==null){
            System.out.println(request.getParameter("editcomment"));
            return "redirect: /../admin/index";
        } else{
            String commentid = request.getParameter("editcomment");
            Integer cid = Integer.valueOf(commentid);
            String content = request.getParameter("comment.content");
            String vote_up = request.getParameter("comment.vote_up");
            Integer vup = Integer.valueOf(vote_up);
            String status = request.getParameter("comment.status");

            ArticleComment articleComment = commentService.SelectCommentById(cid);
            articleComment.setContent(content);
            articleComment.setVote_up(vup);
            articleComment.setStatus(status);
            commentService.UpdateComment(articleComment);
            System.out.println("articleComment="+articleComment);
            return "redirect: /../admin/article/commentEdit" + "?editcomment=" + cid;
        }
    }
    //附件设置
    @RequestMapping("/AttachmentSetting.do")
    public String AttachmentSetting(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        Map<String,Object> options = optionReaderTool.ReadOptions();
        response.setContentType("text/html;charset=UTF-8");
        options.put("photo_limit",request.getParameter("photo_limit"));
        options.put("file_limit",request.getParameter("file_limit"));
        options.put("use_photo_watermark",request.getParameter("use_photo_watermark"));
        options.put("watermark_location",request.getParameter("watermark_location"));
        options.put("watermark_transparency",request.getParameter("watermark_transparency"));
        options.put("watermark_text",request.getParameter("watermark_text"));
        optionService.UpdateAttachmentSetting(options);
        return "redirect: /../admin/attachment/setting";
    }
    //网站设置
    @RequestMapping("/SystemSetting.do")
    public String SystemSetting(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        Map<String,Object> options = optionReaderTool.ReadOptions();
        response.setContentType("text/html;charset=UTF-8");
        options.put("web_name",request.getParameter("web_name"));
        options.put("web_title",request.getParameter("web_title"));
        options.put("web_subtitle",request.getParameter("web_subtitle"));
        options.put("web_describe",request.getParameter("web_describe"));
        options.put("ISPN",request.getParameter("ISPN"));
        options.put("copyright",request.getParameter("copyright"));
        optionService.UpdateSystemSetting(options);
        return "redirect: /../admin/system/setting";
    }
    //注册设置
    @RequestMapping("/RegSetting.do")
    public String RegSetting(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        Map<String,Object> options = optionReaderTool.ReadOptions();
        response.setContentType("text/html;charset=UTF-8");
        options.put("success_logged_url",request.getParameter("success_logged_url"));
        options.put("allow_regist",request.getParameter("allow_regist"));
        options.put("register_status",request.getParameter("register_status"));
        optionService.UpdateSystemRegSetting(options);
        return "redirect: /../admin/system/reg";
    }




}
